package com.free.framework.lib.utils;


import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * @author 李博
 * @date
 */
public class DeviceInfo {

    public static boolean checkPermissions(Context context, String permission) {
        PackageManager localPackageManager = context.getPackageManager();
        return localPackageManager.checkPermission(permission,
                context.getPackageName()) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * Determine the current network type
     *
     * @param context
     * @return
     */
    public static boolean isNetworkTypeWifi(Context context) {

        if (checkPermissions(context, "android.permission.INTERNET")) {
            ConnectivityManager cManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = cManager.getActiveNetworkInfo();

            if (info != null && info.isAvailable()
                    && info.getTypeName().equalsIgnoreCase("WIFI")) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static String getScreenSize(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wManager.getDefaultDisplay().getMetrics(dm);
        float width = dm.widthPixels;
        float height = dm.heightPixels;
        if (width > 0 && height > 0) {
            return height + "_" + width;
        } else {
            return "000_000";
        }
    }


    public static String getDeviceId(Context context) {
        TelephonyManager telephonyManager;
        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imeistring = null;

        if (PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission("android.permission.READ_PHONE_STATE", context.getPackageName())) {
            imeistring = telephonyManager.getDeviceId();
        }

        if (!TextUtils.isEmpty(imeistring)) {
            return imeistring;
        } else {
            return "";
        }
    }

    public static String getMobileService(Context context) {
        String service = "unknown";
        TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        String operator = telManager.getSimOperator();

        if (!TextUtils.isEmpty(operator)) {
            return operator;
        } else {
            return service;
        }

    }


    public static String getNetworkType(Context ctx) {
        if (isNetworkTypeWifi(ctx)) {
            return "WIFI";
        }

        TelephonyManager manager = (TelephonyManager) ctx
                .getSystemService(Context.TELEPHONY_SERVICE);

        String typeString = "UNKNOWN";
        if (null == manager) {
            return typeString;
        }

        int type = manager.getNetworkType();

        if (type == TelephonyManager.NETWORK_TYPE_CDMA) {
            typeString = "CDMA";
        }
        if (type == TelephonyManager.NETWORK_TYPE_EDGE) {
            typeString = "EDGE";
        }
        if (type == TelephonyManager.NETWORK_TYPE_EVDO_0) {
            typeString = "EVDO_0";
        }
        if (type == TelephonyManager.NETWORK_TYPE_EVDO_A) {
            typeString = "EVDO_A";
        }
        if (type == TelephonyManager.NETWORK_TYPE_GPRS) {
            typeString = "GPRS";
        }
        if (type == TelephonyManager.NETWORK_TYPE_HSDPA) {
            typeString = "HSDPA";
        }
        if (type == TelephonyManager.NETWORK_TYPE_HSPA) {
            typeString = "HSPA";
        }
        if (type == TelephonyManager.NETWORK_TYPE_HSUPA) {
            typeString = "HSUPA";
        }
        if (type == TelephonyManager.NETWORK_TYPE_UMTS) {
            typeString = "UMTS";
        }
        if (type == TelephonyManager.NETWORK_TYPE_UNKNOWN) {
            typeString = "UNKNOWN";
        }
        if (type == TelephonyManager.NETWORK_TYPE_1xRTT) {
            typeString = "1xRTT";
        }
        if (type == 11) {
            typeString = "iDen";
        }
        if (type == 12) {
            typeString = "EVDO_B";
        }
        if (type == 13) {
            typeString = "LTE";
        }
        if (type == 14) {
            typeString = "eHRPD";
        }
        if (type == 15) {
            typeString = "HSPA+";
        }
        if (type == 16) {
            typeString = "GSM";
        }
        return typeString;
    }
}
